package com.ming.transaction.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ming.transaction.common.Result;
import com.ming.transaction.entity.Wbuy;
import com.ming.transaction.mapper.WbuyMapper;
import com.ming.transaction.service.IWbuyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 求购商品表 服务实现类
 * </p>
 *
 * @author 陈仕健
 * @since 2023-05-10
 */
@Service
public class WbuyServiceImpl extends ServiceImpl<WbuyMapper, Wbuy> implements IWbuyService {

    @Resource
    private HttpServletRequest request;

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    public Result delBat(List<Integer> ids) {
        if (request.getSession().getAttribute("ROLE").equals("ROLE_USER")){
            return Result.fail("权限不足,请联系管理员！");
        }
        return Result.success("批量删除成功！",removeByIds(ids));
    }

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @param product
     * @param unumber
     * @return
     */
    @Override
    public Result pageSach(Integer pageNum, Integer pageSize, String product, String unumber) {
        IPage<Wbuy> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Wbuy> queryWrapper = new QueryWrapper<>();
        if (!"".equals(product)){
            queryWrapper.like("product",product);
        }
        if (!"".equals(unumber)){
            queryWrapper.like("unumber",unumber);
        }
        return Result.success("求购列表分页查询!",page(page,queryWrapper));
    }

    /**
     * 保存求购信息
     * @param wbuy
     * @return
     */
    @Override
    public Result savePuct(Wbuy wbuy) {
        if (save(wbuy)) {
            return Result.success("添加求购信息成功！");
        }
        return Result.fail("添加求购信息失败！");
    }

    /**
     * 更新求购信息
     * @param wbuy
     * @return
     */
    @Override
    public Result upPuct(Wbuy wbuy) {
        if (saveOrUpdate(wbuy)) {
            return Result.success("更新求购信息成功!");
        }
        return Result.fail("添加求购信息失败！");

    }

    /**
     * 删除求购信息
     * @param id
     * @return
     */
    @Override
    public Result delById(Integer id) {
        if (removeById(id)) {
            return Result.success("删除求购信息成功！");
        }
        return Result.fail("删除求购信息失败！");
    }
}
